package problem;

import Util.DBUtil;

import javafx.scene.web.PromptData;


import javax.print.attribute.standard.PresentationDirection;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

//DAO数据访问对象
//包含增删改查方法,用来操作数据库
//进而进行题目的相关操作
public class ProblemDAO {//Data Access Object
    //往数据库插入一条记录
    public void insert(Problem problem){
        //1.和数据库建立连接
        Connection connection = DBUtil.getConnection();
        PreparedStatement statement = null;
        try {
            //2.拼装sql语句
            String sql = "insert into oj_table values(null,?,?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1,problem.getTitle());
            statement.setString(2,problem.getLevel());
            statement.setString(3,problem.getDescription());
            statement.setString(4,problem.getTemplateCode());
            statement.setString(5,problem.getTestCode());
            //执行sql
            statement.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        finally{
                DBUtil.close(connection,statement,null);
        }

    }
    //从数据库删除一条记录
    public void delete(int problemId){
        //1.建立数据库连接
        Connection connection = DBUtil.getConnection();
        //2.拼装SQL
        PreparedStatement statement = null;
        String sql = "delete from oj_table where id = ?";
        try {
            statement = connection.prepareStatement(sql);
            statement.setInt(1,problemId);
            //3.执行sql
            statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null);
        }
    }
    //查找全部题目(用来实现题目列表页)
    //只查找Problem的一部分字段即可
    // id title level
    //正常来说一个系统的"查找全部操作"一般都支持分页查找
    //不分页的话,数据量可能会比较大,影响程序的效率
    //此程序不分页
    //后端实现分页是很容易的,前端比较困难
    //SQL中本来就有limit offset这样的支持
    public List<Problem> selectAll(){
        List<Problem> problems = new ArrayList<>();
        //1.建立连接
        Connection connection = DBUtil.getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        //2.拼装sql
        String sql = "select id,title,level from oj_table";
        try {
            statement = connection.prepareStatement(sql);
            //3.执行sql
             resultSet = statement.executeQuery();
            //4.遍历结果集合
            while(resultSet.next()){
                //每次从结果中提取一个对象
                Problem problem = new Problem();
                problem.setId(resultSet.getInt("id"));
                problem.setTitle(resultSet.getString("title"));
                problem.setLevel(resultSet.getString("level"));
                /*problem.setDescription(resultSet.getString("description"));
                problem.setTemplateCode(resultSet.getString("templateCode"));
                problem.setTestCode(resultSet.getString("testCode"));*/
                problems.add(problem);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return problems;
}
    //查找指定题目(用来实现题目详情页)
    //需要把Problem中的每个字段都查询出来
    public static Problem selectOne(int problemId){
        //建立连接
        Connection connection = DBUtil.getConnection();
        //拼装sql
        String sql = "select * from oj_table where id = ?";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setInt(1,problemId);
            //执行sql
            resultSet = statement.executeQuery();
            //遍历结果集,由于查询结果要么是0要么是1
            //直接用if判定
            if(resultSet.next()){
                Problem problem = new Problem();
                problem.setId(resultSet.getInt("id"));
                problem.setTitle(resultSet.getString("title"));
                problem.setLevel(resultSet.getString("level"));
                problem.setDescription(resultSet.getString("description"));
                problem.setTemplateCode(resultSet.getString("templateCode"));
                problem.setTestCode(resultSet.getString("testCode"));
              return problem;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally{
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }

    private static void testInsert(){
        Problem problem = new Problem();
        problem.setId(2);
        problem.setTitle("两数之和");
        problem.setLevel("简单");
        problem.setDescription("给定一个整数数组 nums 和一个整数目标值 target，请你在该数组中找出 和为目标值 target  的那 两个 整数，并返回它们的数组下标。\n" +
                "\n" +
                "你可以假设每种输入只会对应一个答案。但是，数组中同一个元素在答案里不能重复出现。\n" +
                "\n" +
                "你可以按任意顺序返回答案。\n" +
                "\n" +
                " \n" +
                "\n" +
                "示例 1：\n" +
                "\n" +
                "输入：nums = [2,7,11,15], target = 9\n" +
                "输出：[0,1]\n" +
                "解释：因为 nums[0] + nums[1] == 9 ，返回 [0, 1] 。\n" +
                "示例 2：\n" +
                "\n" +
                "输入：nums = [3,2,4], target = 6\n" +
                "输出：[1,2]\n" +
                "示例 3：\n" +
                "\n" +
                "输入：nums = [3,3], target = 6\n" +
                "输出：[0,1]\n" +
                " \n" +
                "\n" +
                "提示：\n" +
                "\n" +
                "2 <= nums.length <= 104\n" +
                "-109 <= nums[i] <= 109\n" +
                "-109 <= target <= 109\n" +
                "只会存在一个有效答案\n" +
                "\n" +
                "来源：力扣（LeetCode）\n" +
                "链接：https://leetcode-cn.com/problems/two-sum\n" +
                "著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。");
        problem.setTemplateCode("class Solution {\n" +
                "    public int[] twoSum(int[] nums,int target){\n" +
                "        return null;\n" +
                "    }");
        problem.setTestCode("public static void main(String[] args) {\n" +
                "        Solution solution = new Solution();\n" +
                "        int[] arr = {2,7,11,15};\n" +
                "        int target = 9;\n" +
                "        int[] result = solution.twoSum(arr,target);\n" +
                "        if(result.length == 2 && result[0] == 1 && result[1] == 2){\n" +
                "            System.out.println(\"TestCase OK!\");\n" +
                "        }\n" +
                "        else{\n" +
                "            System.out.println(\"TestCase Failed! arr:{2,7,11,15},target:9\");\n" +
                "        }\n" +
                "        int[] arr2 = {3,2,4};\n" +
                "        int target2 = 6;\n" +
                "        int[] result2 = solution.twoSum(arr2,target2);\n" +
                "        if(result2.length == 2 && result2[0] == 1 && result2[1] == 2){\n" +
                "            System.out.println(\"TestCase OK!\");\n" +
                "        }\n" +
                "        else{\n" +
                "            System.out.println(\"TestCase Failed! arr:{3,2,4},target:6\");\n" +
                "        }\n" +
                "    }");
        ProblemDAO problemDAO = new ProblemDAO();
        problemDAO.insert(problem);
    }
    private static void testDelete(){
        ProblemDAO problemDAO = new ProblemDAO();
        problemDAO.delete(9);
    }
    private static void testSelectAll(){
        ProblemDAO problemDAO = new ProblemDAO();
        List<Problem> problems = problemDAO.selectAll();
        System.out.println(problems);
    }
    private static void testSelectOne(){
        ProblemDAO problemDAO = new ProblemDAO();
        Problem problem =  ProblemDAO.selectOne(4);
        System.out.println(problem);
    }
    public static void main(String[] args) {
        testInsert();
        //testDelete();
        //testSelectAll();
        //testSelectOne();
    }
}
